#include "StaticTrimesh.h"

StaticTrimesh::StaticTrimesh()
{
	data = dGeomTriMeshDataCreate();
}

StaticTrimesh::StaticTrimesh(dSpaceID &space, std::vector<Vertex> &verts, std::vector<GLuint> &indices) : vertices(verts), indices(indices)
{
	data = dGeomTriMeshDataCreate();

	dGeomTriMeshDataBuildSingle(data, (dReal*)&StaticTrimesh::vertices[0].position[0], sizeof(Vertex), StaticTrimesh::vertices.size(), (dTriIndex*)&StaticTrimesh::indices[0], StaticTrimesh::indices.size(), sizeof(GLuint) * 3);

	geom = dCreateTriMesh(space, data, 0, 0, 0);
}

StaticTrimesh::~StaticTrimesh()
{}

void StaticTrimesh::render()
{
	glColor3ubv(color);

	GLfloat mat_specular[] = {0.1f, 0.1f, 0.1f};
	glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);

	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_NORMAL_ARRAY);

	glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &vertices[0].position);
	glNormalPointer(GL_FLOAT, sizeof(Vertex), &vertices[0].normal);

	glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, &indices[0]);

	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_NORMAL_ARRAY);
}